﻿using KinonekoSoftware.CodariaDB.Documents.TemplateContents;
using KinonekoSoftware.CodariaDB.Languages;
using KinonekoSoftware.UI;
using KinonekoSoftware.Foundation.UI.Media;

namespace KinonekoSoftware.Studio.Pages.Instances.Templated
{
    public sealed class NewColoredKeywordViewModel : ObjectDialog<ColoredKeyword>
    {
        private string          _content;
        private Color           _color;
        private SolidColorBrush _brush;
        private ColoredKeyword  _target;

        public static Task<Result<ColoredKeyword>> New()
        {
            return Dialog<ColoredKeyword>(new NewColoredKeywordViewModel());
        }
        
        
        public static Task<Result<ColoredKeyword>> Edit( ColoredKeyword keyword)
        {
            var context = new DialogContext();
            context.Set(ContextBase.Key_Value, keyword);
            return Dialog<ColoredKeyword>(new NewColoredKeywordViewModel(), context);
        }

        public NewColoredKeywordViewModel()
        {
            Color = MediaFactory.ToColor(BrushKey.ForegroundLevel1);
        }
        
        protected override bool OnReceive(DialogContext context)
        {
            if (context.Has(ContextBase.Key_Value))
            {
                _target = context.GetObject<ColoredKeyword>(ContextBase.Key_Value);
                Content = _target.Content;
                Color   = MediaFactory.ToColor(_target.Color);
                return true;
            }

            return false;
        }

        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(
                DialogRule.Create<NewColoredKeywordViewModel>(
                    CodariaSR.Text_Parameter_EmptyName, 
                    x => !string.IsNullOrEmpty(x.Content)));
        }
        
        protected override ColoredKeyword OnFinish(bool editing)
        {
            if (editing)
            {
                _target.Content = Content;
                _target.Color   = Color.ToString();
                return _target;
            }
            
            return new ColoredKeyword
            {
                Content = Content,
                Color   = Color.ToString(),
            };
        }
        
        /// <summary>
        /// 获取或设置 <see cref="Color"/> 属性。
        /// </summary>
        public Color Color
        {
            get => _color;
            set
            {
                TryFinishAndSetValue(ref _color, value);
                Brush = new SolidColorBrush(value);
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="Brush"/> 属性。
        /// </summary>
        public SolidColorBrush Brush
        {
            get => _brush;
            private set => SetValue(ref _brush, value);
        }
        /// <summary>
        /// 获取或设置 <see cref="Content"/> 属性。
        /// </summary>
        public string Content
        {
            get => _content;
            set => TryFinishAndSetValue(ref _content, value);
        }
    }
}